﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH;
using Module.Category.BL;
using QLBH.DTO;

namespace Module.Category
{
    public partial class CalculateUnitManagement : ViewBase
    {
        private static CalculateUnitManagement instance;

        public static CalculateUnitManagement Instance
        {
            get 
            {
                if (instance==null)
                {
                    instance = new CalculateUnitManagement();
                }
                return CalculateUnitManagement.instance; 
            }
            
        }
        public CalculateUnitManagement()
        {
            InitializeComponent();
        }

        private void CalculateUnitManagement_Load(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.DataSource = CalculateUnitManagementBL.Instance.GetAllCalUnits();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AddCalUnitDlg addcalUnit = new AddCalUnitDlg();
                if (addcalUnit.ShowDialog() == DialogResult.OK)
                {
                    
                    dataGridViewX1.DataSource = CalculateUnitManagementBL.Instance.GetAllCalUnits();
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
        }
        private void EditCalUnit()
        {
            try
            {
                EditCalUnitDlg editcalUnit = new EditCalUnitDlg();
                editcalUnit.CalUnit = dataGridViewX1.CurrentRow.DataBoundItem as DVTDTO;

                if (editcalUnit.ShowDialog() == DialogResult.OK)
                {
                    CalculateUnitManagementBL.Instance.UpdateCalUnit(editcalUnit.CalUnit);

                    dataGridViewX1.DataSource = CalculateUnitManagementBL.Instance.GetAllCalUnits();

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditCalUnit();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try
            {
                List<DVTDTO> selectedObjs = new List<DVTDTO>();
                foreach (DataGridViewRow row in dataGridViewX1.SelectedRows)
                {
                    selectedObjs.Add(row.DataBoundItem as DVTDTO);
                }

                if (selectedObjs.Count > 0)
                {
                    CalculateUnitManagementBL.Instance.DeleteCalUnits(selectedObjs);

                    dataGridViewX1.DataSource = CalculateUnitManagementBL.Instance.GetAllCalUnits();

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.DataSource = CalculateUnitManagementBL.Instance.GetAllCalUnits();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditCalUnit();
        }

        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            if (dataGridViewX1.RowCount == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
            }
            else
            {
                buttonEdit.Enabled = true;
                buttonDelete.Enabled = true;
            }
        }
    }
}
